package com.sxj.corejava.code08_常用类.Object类;

/**
 * @author 石小俊
 * @date 2025年01月10日
 */
public class Test01_常用方法 {

    public static void main(String[] args) throws CloneNotSupportedException {
//        getClassTest();
//        equalsTest();
//        hashCodeTest();
//        toStringTest();
//        cloneTest();
        finalizeTest();
    }

    public static void getClassTest() {
        // getClass:返回指定指定的运行时类型,返回的class类型
        Person person = new Person();
        // 当前的Person类没有继承任何类,默认继承了Object
        // 可以调用Object类中的方法
        System.out.println(person.getClass());

        Person person2 = new Man();
        System.out.println(person2.getClass());
        Person person3 = new WoMan();
        System.out.println(person3.getClass());

        if (person2 instanceof Man) {
            Man man = (Man) person2;
            man.work();
        }

        // 可以通过类名.class属性获取当前类的class类型
        System.out.println(Person.class);
        System.out.println(Man.class);
        System.out.println(WoMan.class);

        System.out.println(person.getClass() == Person.class);
        System.out.println(person2.getClass() == Man.class);
        System.out.println(person3.getClass() == WoMan.class);
        System.out.println(person2.getClass() == WoMan.class);

        if (person3.getClass() == WoMan.class) {
            WoMan woMan = (WoMan) person3;
            woMan.shopping();
        }
    }

    public static void equalsTest() {
        Person person = new Person("admin", 18);
        Person person2 = new Person("admin", 18);
        // == 比较的是栈中的值
        System.out.println(person == person2);
        // 在Object方法中的equalse本质上就是==
        // 如果想要比较对象的值是否相等,可以选择重写equals方法
        // 根据你的需求进行重写
        System.out.println(person.equals(person2));

        int i = 1;
        int i2 = 1;
        System.out.println(i == i2);

        // 直接赋值的字符串存储在常量池中
        String s1 = "admin";
        String s2 = "admin";
        System.out.println(s1 == s2);
        String s3 = new String("admin");
        String s4 = new String("admin");
        System.out.println(s3 == s4);
        System.out.println(s3.equals(s4));
    }

    public static void hashCodeTest() {
        Person person = new Person("admin", 18);
        Person person2 = new Person("admin", 18);
        System.out.println(person.hashCode());
        System.out.println(person2.hashCode());
        System.out.println(person.equals(person2));
    }

    public static void toStringTest() {
        Person person = new Person("admin", 18);
        System.out.println(person);
        System.out.println(person.getClass().getName());
        System.out.println(person.getClass().getName() + "@" + Integer.toHexString(person.hashCode()));
        // 查看源代码中的toString方法
        //  return getClass().getName() + "@" + Integer.toHexString(hashCode());
        // 我们可以看出直接打印对象本质上就是调用了对象的toString方法
//        System.out.println(person.toString());
    }

    public static void cloneTest() throws CloneNotSupportedException {
        Address address = new Address("江苏", "南京");
        Person person = new Person("admin", 18, address);
        Person person2 = (Person) person.clone();
        System.out.println(person);
        System.out.println(person2);
        System.out.println(person == person2);
        System.out.println(person.equals(person2));

        // 克隆方法属于浅克隆s
        person2.getAddress().setCity("苏州");
        person.getAddress().setProvince("浙江");
        person.getAddress().setCity("杭州");
        person.setName("jack");
        person.setAge(20);
        System.out.println(person);
        System.out.println(person2);
    }

    public static void finalizeTest() {
        Person person = new Person();
        Person person2 = person;
        person = null;
        person2 = null;

        // 主动建议gc回收垃圾
//        System.gc();
        // 该方法是一个本地方法
        Runtime.getRuntime().gc();
    }
}
